package com.ruibei.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruibei.R;
import com.ruibei.models.SortModel;
import com.ruibei.ui.adapter.SortAdapter;
import com.ruibei.utils.CharacterParser;
import com.ruibei.utils.PinyinComparator;
import com.ruibei.utils.ToastUtils;
import com.ruibei.view.SideBarView;
import com.ruibei.view.TitleBarView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 选择审批人页面
 * 
 * @author limingyan
 * 
 */
public class ContactsActivity extends BaseActivity {

	@InjectView(R.id.contact_title_bar)
	TitleBarView mTitleBarView;
	@InjectView(R.id.contact_list)
	ListView mListView;
	@InjectView(R.id.sidebar)
	SideBarView mBarView;
	@InjectView(R.id.dialog)
	TextView mDialog;

	// 汉字转换成拼音的类
	private CharacterParser mCharacterParser;
	// 根据拼音来排列ListView里面的数据类
	private PinyinComparator mPinyinComparator;
	private SortAdapter mAdapter;
	private List<SortModel> mSourceDateList = new ArrayList<SortModel>();

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_contacts);
		ButterKnife.inject(this);

		setUpView();
	}

	private void setUpView() {
		mTitleBarView.setCommonTitle(View.VISIBLE, View.VISIBLE, View.GONE);
		mTitleBarView.setTitleText("审批人列表");
		mTitleBarView.setBtnLeft(R.drawable.back, R.string.back);
		mTitleBarView.setBtnLeftOnclickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mCharacterParser = CharacterParser.getInstance();
		mPinyinComparator = new PinyinComparator();
		mBarView.setTextView(mDialog);

		loadData();
		
		mAdapter = new SortAdapter(this, mSourceDateList);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(onItemClickListener);
	}

	private void loadData() {
		// 测试数据
		String[] names = new String[] { "黄总", "李明炎", "吴娟" };
		mSourceDateList = filledData(names);
		// 根据a-z进行排序源数据
		Collections.sort(mSourceDateList, mPinyinComparator);
	}

	/**
	 * 点击list监听
	 */
	private OnItemClickListener onItemClickListener = new OnItemClickListener() {
		@SuppressLint("NewApi")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// 这里要利用adapter.getItem(position)来获取当前position所对应的对象
			ToastUtils.showShort(((SortModel) mAdapter.getItem(position))
					.getName());
			String[] to = new String[] { "contact",
					((SortModel) mAdapter.getItem(position)).getName() };
			// Intent intent = new Intent(ContactsActivity.this,
			// SingleChatActivity.class);
			// intent.putExtra("give_contact", to);
			// startActivity(intent);
		}
	};

	/**
	 * 为ListView填充数据
	 * 
	 * @param date
	 * @return
	 */
	private List<SortModel> filledData(String[] date) {
		List<SortModel> mSortList = new ArrayList<SortModel>();

		for (int i = 0; i < date.length; i++) {
			SortModel sortModel = new SortModel();
			sortModel.setName(date[i]);
			// 汉字转换成拼音
			String pinyin = mCharacterParser.getSelling(date[i]);
			String sortString = pinyin.substring(0, 1).toUpperCase();

			// 正则表达式，判断首字母是否是英文字母
			if (sortString.matches("[A-Z]")) {
				sortModel.setSortLetters(sortString.toUpperCase());
			} else {
				sortModel.setSortLetters("#");
			}

			mSortList.add(sortModel);
		}
		return mSortList;
	}
}
